package com.jmu.medicineknows.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmu.medicineknows.dto.DrugPageDto;
import com.jmu.medicineknows.dto.SearchDto;
import com.jmu.medicineknows.entity.*;
import com.jmu.medicineknows.mapper.SaleCountMapper;
import com.jmu.medicineknows.mapper.SaleDateMapper;
import com.jmu.medicineknows.mapper.SaleRecordMapper;
import com.jmu.medicineknows.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.RescaleOp;
import java.util.List;
import java.util.Objects;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    SaleCountMapper saleCountMapper;
    @Autowired
    SaleDateMapper saleDateMapper;
    @Autowired
    SaleRecordMapper saleRecordMapper;
    @Override
    public Result getAll() {
        return Result.defaultSuccess(saleCountMapper.selectList(null));

    }

    @Override
    public Result getAllByDate() {
        return Result.defaultSuccess(saleDateMapper.selectList(null));

    }

    @Override
    public Result getRecords(DrugPageDto pageDto) {
        Page<SaleRecord> saleRecordPage =new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize());
        LambdaQueryWrapper<SaleRecord> wrapper =new LambdaQueryWrapper<>();
        if(pageDto.getFlag()=='1'){
            wrapper.like(SaleRecord::getName,pageDto.getContent());
        }else if(pageDto.getFlag()=='2'){
            wrapper.like(SaleRecord::getCode,pageDto.getContent());
        }else {
            wrapper.like(SaleRecord::getNumber,pageDto.getContent());
        }
        wrapper.between(SaleRecord::getDate, pageDto.start,pageDto.end);
        wrapper.orderByAsc(SaleRecord::getDate);
//        wrapper.eq(SaleRecord::getCode,"236703");
        IPage<SaleRecord> saleRecordIPage = saleRecordMapper.selectPage(saleRecordPage,wrapper);
        return Result.defaultSuccess(saleRecordIPage);
    }

    @Override
    public Result deleteRecord(Integer id) {
        LambdaQueryWrapper<SaleRecord> wrapper =new LambdaQueryWrapper();
        wrapper.eq(SaleRecord::getId,id);
        return Result.defaultSuccess(saleRecordMapper.delete(wrapper));
    }
    @Override
    public Result updateRecord(SaleRecord saleRecord) {
        LambdaQueryWrapper<SaleRecord> wrapper =new LambdaQueryWrapper();
        System.out.println(saleRecord);
        wrapper.eq(SaleRecord::getId,saleRecord.getId());
        return Result.defaultSuccess(saleRecordMapper.update(saleRecord,wrapper));
    }
    @Override
    public Result searchByFlag(SearchDto searchDto) {

//        LambdaQueryWrapper<SaleRecord> wrapper =new LambdaQueryWrapper();
//        System.out.println(saleRecord);
//        wrapper.eq(SaleRecord::getId,saleRecord.getId());
//        return Result.defaultSuccess(saleRecordMapper.update(saleRecord,wrapper));
        return null;
    }
}
